package edu.uwm.cs552;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class GoalCard extends Observable {
	public static final int NUM_GOALS = 3;
	
	private final Goal[] goals;
	private int selected = -1;
	
	private GoalCard(Goal[] gs) {
		goals = gs;
	}
	
	public Goal getGoal(int i) {
		return goals[i];
	}
	
	public int getSelected() { return selected; }

	/**
	 * @param index
	 */
	public void setSelected(final int index) {
		if (index != selected) setChanged();
		selected = index;
		notifyObservers();
	}

	public static GoalCard generate(HexBoard board) {
		Goal[] goals = new Goal[NUM_GOALS];
		for (int i=0; i < NUM_GOALS; ++i) {
			goals[i] = Goal.generate(board);
		}
		return new GoalCard(goals);
	}
	
	public class Panel extends JPanel {
		/**
		 * Keep Eclipse Happy
		 */
		private static final long serialVersionUID = 1L;
		public Panel() {
			ActionListener listener = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					final int index = ((RadioButton)arg0.getSource()).getIndex();
					setSelected(index);
				}
			};
			ButtonGroup g = new ButtonGroup();
			setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
			
			for (int i=0; i < NUM_GOALS; ++i) {
				RadioButton b = new RadioButton(i);
				b.addActionListener(listener);
				g.add(b);
				JPanel goalPanel = new JPanel();
				goalPanel.add(b);
				goalPanel.add(getGoal(i).asLabel());
				goalPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
				add(goalPanel);
			}
			setBorder(BorderFactory.createLineBorder(Color.BLACK));
		}
		
		public GoalCard getGoalCard() {
			return GoalCard.this;
		}
	}
	
	private class RadioButton extends JRadioButton {
		/**
		 * Keep Eclipse happy
		 */
		private static final long serialVersionUID = 1L;
		private final int index;
		public RadioButton(int i) {
			super();
			index = i;
		}
		public int getIndex() {
			return index;
		}
	}
}
